<?php

class accordion extends components {
    
    private $accordionElements = array();
    
    public function init($page, $tag) {
        
        $this->name = '123';
        
        # Add the UI javascript file
        $page->addJavascriptFile('jquery/jquery.ui/js/jquery-ui-1.8.18.custom.min',true);
        $page->addCssFile('../javascript/jquery/jquery.ui/css/smoothness/jquery-ui-1.8.18.custom',true);
        
        parent::init($page, $tag);
        
    }
    
    public function addAccordionElement($title, $content) {
        
        # Add an element
        $this->accordionElements[$title] = $content;
        
    }
    
    public function generate($page, $tag) {
        
        # Store all accordion elements in here
        $accordionHtml = '<div id="accordion'.$this->name.'">';
        
        # Start building html
        foreach($this->accordionElements as $title => $content) {
            
            $accordionHtml .= '<h3><a href="#">'.$title.'</a></h3>';
            $accordionHtml .= '<div>'.$content.'</div>';
            
        }
        
        $accordionHtml .= '</div>';
        
        $javascript = "
            $(function() {
		$('#accordion".$this->name."').accordion();
            });

            $(document).ready(function(){
                $('.accordion".$this->name." .head').click(function() {
                        $(this).next().toggle('slow');
                        return false;
                }).next().hide();
            });
        ";
        
        $page->append($accordionHtml,$tag);
        $page->append($javascript,'javaEnd');
        
        parent::generate($page, $tag);
        
    }
    
}

?>
